import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/model/use_request/use_request_model.dart';
import 'package:ls_work/net/response/use_request/goods_request_list_data.dart';
import 'package:ls_work/net/response/use_request/goods_request_type_list.dart';

class ChooseGoodsViewModel extends BaseViewModel {
  final dynamic useRange;
  ChooseGoodsViewModel(BuildContext context,{this.useRange}) : super(context);

  var model = UseRequestModel();

  var typeList = <GoodsRequestTypeList>[];
  var goodsList = <GoodsRequestListData>[];
  GoodsRequestTypeList? selectType;

  @override
  void onCreated() {
    getGoodsTypeList();
  }

  Future<void> getGoodsTypeList() async {
    showLoadingDialog();
    try {
      typeList = await model.goodsTypeList();
      hideDialog();
      if (typeList.isNotEmpty) {
        selectType = typeList[0];
        try{
          goodsList =
          await model.goodsList(materialType: selectType?.id.toString()??"");
        }catch(e){
          showToast(NetErrorUtils.getNetError(e));
        }
        notifyListeners();
      } else {
        showToast("物资分类为空");
        showEmptyState(msg: "物资分类为空");
      }
    } catch (e) {
      hideDialog();
      showToast(NetErrorUtils.getNetError(e));
      showErrorState(NetErrorUtils.getNetError(e));
    }
  }


  Future<void> typeClick(GoodsRequestTypeList type) async {
    selectType = type;
    showLoadingDialog();
    try{
      goodsList =
          await model.goodsList(materialType: selectType?.id.toString()??"",useRange: useRange.toString());
      notifyListeners();
      hideDialog();
    }catch(e){
      hideDialog();
      showToast(NetErrorUtils.getNetError(e));
    }
  }
}
